<?php
// 提交诊单的信息
// header("content-type:text/html;charset=UTF-8");
include_once("conn.php");
include_once("d_getLoginInfo.php");

$_SESSION["now_cases_id"] = "";

//设置中国时区
date_default_timezone_set('Asia/Shanghai');
// 获取当前日期
$over_Date = date('Y-m-d');

$cases_ID = $_POST["cases_ID"];
$cases_petName = $_POST["cases_petName"];
$cases_petType = $_POST["cases_petType"];
$cases_orderType = $_POST["cases_orderType"];
$cases_result = $_POST["caese_result"];
$caese_deal = $_POST["caese_deal"];

$cases_petID = $_POST["cases_petID"];
$cases_userID = $_POST["cases_userID"];

$insert_cases = "INSERT INTO cases_infos
    (cases_ID,cases_docID,cases_docName,cases_userID,cases_petID,cases_petName,cases_petType,cases_orderType,cases_result,cases_deal,cases_date)
    VALUES
    ('" . $cases_ID . "','" . $doc_ID . "','" . $doc_Name . "','" . $cases_userID . "','" . $cases_petID . "','" . $cases_petName . "','" . $cases_petType . "',
    '" . $cases_orderType . "','" . $cases_result . "','" . $caese_deal . "','" . $over_Date . "')";
if (mysqli_query($connect_DB, $insert_cases)) {
    // echo "<script>alert('提交诊单信息成功！');location='doc_AcceptInfo.php';</script>";
    $select_orderinfos = "SELECT * FROM order_infos JOIN department_infos ON order_infos.orderType = department_infos.department_ID  WHERE orderID = '" . $cases_ID . "'";
    if ($result_orderinfos = mysqli_query($connect_DB, $select_orderinfos)) {
        $row_orderinfos = mysqli_fetch_object($result_orderinfos);

        $insert_history = "INSERT INTO order_history_infos
        (order_ID,user_ID,doc_ID,pet_ID,order_Type,order_Remark,sub_Date,order_Date,over_Date,orderState)
        VALUES
        ('" . $cases_ID . "','" . $row_orderinfos->user_ID . "','" . $row_orderinfos->doc_ID . "','" . $row_orderinfos->pet_ID . "','" . $row_orderinfos->orderType . "-" . $row_orderinfos->department_Name . "',
        '" . $row_orderinfos->orderRemark . "','" . $row_orderinfos->subDate . "','" . $row_orderinfos->orderDate . "','" . $over_Date . "','1')";
        if (mysqli_query($connect_DB, $insert_history)) {
            $del_order = "DELETE FROM order_infos WHERE orderID = '" . $cases_ID . "'";
            if (mysqli_query($connect_DB, $del_order)) {
                echo "<script>alert('提交诊单信息成功！');location='doc_AcceptInfo.php';</script>";
            } else {
                $delhis = "DELETE FROM order_history_infos WHERE order_ID = '" . $cases_ID . "'";
                $delcases = "DELETE FROM cases_infos WHERE cases_ID = '" . $cases_ID . "'";
                mysqli_query($connect_DB, $delhis);
                mysqli_query($connect_DB, $delcases);
                echo "<script>alert('删除预约信息失败！');location='doc_AcceptInfo.php';</script>";
            }
        } else {
            $del = "DELETE FROM cases_infos WHERE cases_ID = '" . $cases_ID . "'";
            mysqli_query($connect_DB, $del);
            echo "<script>alert('插入历史预约信息失败！');location='doc_AcceptInfo.php';</script>";
        }
    } else {
        echo "<script>alert('查询基本信息失败！');location='doc_AcceptInfo.php';</script>";
    }
} else {
    echo "<script>alert('提交诊单信息失败！');location='doc_AcceptInfo.php';</script>";
    // location='doc_AcceptInfo.php';
}

$connect_DB->close();
?>